package it.scambiocorse.rowmapper;

import it.scambiocorse.model.AutistaType;
import it.scambiocorse.utils.Utils;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.RowMapper;

public class AutistaRowMapper implements RowMapper {
	final Logger logger = Logger.getLogger(getClass());

  public Object mapRow(ResultSet rs, int row) throws SQLException {
  	String metodo ="mapRow(...)";
  	
  		AutistaType at = null;    
   try {
	    at = new AutistaType();    
    	if (row > -1) { // PROVENGO DALLA TABELLA

    		at.setIdAutista(rs.getInt("id_autista"));
    		at.setIdAutistaUser(rs.getInt("id_autista_user"));
    		at.setIdUserInse(rs.getInt("id_user_inse"));
    		at.setNome(Utils.isValid(rs.getString("nome")) ? rs.getString("nome") : "");
    		at.setTel(Utils.isValid(rs.getString("tel")) ? rs.getString("tel") : "");
    		at.setEmail(Utils.isValid(rs.getString("email")) ? rs.getString("email") : "");
    		at.setDataInse(rs.getTimestamp("data_inse"));
    		at.setNote((Utils.isValid(rs.getString("note")) ? rs.getString("note") : ""));


    		
    	} else {
        	// PROVENGO DALLA VIEW (ServizioRowMapper)
    		at.setIdAutista(rs.getInt("id_autista"));
    		at.setIdUserInse(rs.getInt("id_user_inse_autista"));
    		at.setNome(Utils.isValid(rs.getString("nome_autista")) ? rs.getString("nome_autista") : "");
    		at.setTel(Utils.isValid(rs.getString("tel_autista")) ? rs.getString("tel_autista") : "");
    		at.setEmail(Utils.isValid(rs.getString("email_autista")) ? rs.getString("email_autista") : "");
    		at.setDataInse(rs.getTimestamp("data_inse_autista"));

    	}
	} catch (SQLException e) {
		if (e.getSQLState().equals("S0022")) {
			logger.debug("Colonna non trovata, mappaggio non necessario");
		} else {
			throw e;
		}
	}

    
    return at;
  }
}
